package design.mode.dispatcher.util.excel;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.lang.reflect.Field;
import java.util.*;

/**
 * Java,利用反射读取对象属性，并将数据导出为Excel,对值相同的列进行合并单元格
 * @param <T>
 */
public class ExcelUtil<T> {
    public Workbook exportToExcel(List<Object> productList, String[] content, String title) throws IllegalAccessException {
        if (productList.size() > 0) {
            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet(title);
            CellStyle cellStyle = workbook.createCellStyle();
            //水平对齐方式
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
            // 设置水平居中
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            cellStyle.setWrapText(true);

            Row titleRow = sheet.createRow(0);
            Cell cell = titleRow.createCell(0);
            cell.setCellStyle(cellStyle);
            cell.setCellValue(title);
            sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, content.length - 1));

            Row firstRow = sheet.createRow(1);
            for (int i = 0; i < content.length; i++) {
                sheet.setColumnWidth(i, 20 * 256);
                Cell contentCell = firstRow.createCell(i);
                contentCell.setCellStyle(cellStyle);
                contentCell.setCellValue(content[i]);
            }

            Row contentRow = sheet.createRow(2);
            Field[] declaredFields = productList.get(0).getClass().getDeclaredFields();
            //填充字段名
            for (int i = 0; i < declaredFields.length; i++) {
                Field declaredField = declaredFields[i];
                declaredField.setAccessible(true);
                Cell titleCell = contentRow.createCell(i);
                titleCell.setCellStyle(cellStyle);
                titleCell.setCellValue(declaredField.getName());
            }

//            Map<Integer, MergedRegion> mergedRegionMap = new HashMap<>();
            int rowIndex = 2;
            for (Object object : productList) {
                Row dataRow = sheet.createRow(rowIndex++);
                //将字段名替换为实际内容
                for (int i = 0; i < declaredFields.length; i++) {
                    Field declaredField = declaredFields[i];
                    Object value = declaredField.get(object);

                    Cell dataCell = dataRow.createCell(i);
                    dataCell.setCellStyle(cellStyle);
                    dataCell.setCellValue(value.toString());
//                    MergedRegion mergedRegion = mergedRegionMap.get(i);
//                    if (null == mergedRegion || !Objects.equals(mergedRegion.getLastValue(), value)) {
//                        mergedRegionMap.put(i, new MergedRegion(value, dataCell));
//                    } else {
//                        int rowNumber = mergedRegion.getFirstCell().getRowIndex();
//                        for (int j = 0; j < sheet.getNumMergedRegions(); j++) {
//                            CellRangeAddress existingRange = sheet.getMergedRegion(j);
//                            if (existingRange.isInRange(rowNumber, i)) {
//                                sheet.removeMergedRegion(j);
//                                break;
//                            }
//                        }
//                        dataCell.setCellStyle(mergedRegion.getFirstCell().getCellStyle());
//                        sheet.addMergedRegion(new CellRangeAddress(rowNumber, dataCell.getRowIndex(), i, i));
//                    }
                }
            }
            return workbook;
        }
        return null;
    }

}
